import { defineStore } from 'pinia';
import _ from 'lodash';
import { getKrJsonParamAllByPage, insertKrJsonParam, updateKrJsonParamById } from '@renderer/api/networkApi/KrJsonParamApi';
import { TYPE_SVN_PROJECT } from '@renderer/system/typeConsts';
import { getAllBySelectType } from '@renderer/api/networkApi/KrCodeCommitsApi';

export const useVersionControlStore = defineStore('versionControl', {
    state: () => ({
        codeCommitsList: [], // kr_code_commits 提交注释记录保存
        svnProjectList: [], // svn项目list
    }),
    actions: {
        async refreshCodeCommitsList(selectType) {
            // 数据库读取提交历史
            let codeCommitsRes = await getAllBySelectType({ size: -1, rsrvStr1: selectType });
            this.codeCommitsList = pageResponseDeal(codeCommitsRes) || [];
        },
        async refreshSvnProjectList() {
            let svnConfigRes = await getKrJsonParamAllByPage({ type: TYPE_SVN_PROJECT, size: -1 });
            let svnConfigList = pageResponseDeal(svnConfigRes);
            let svnConfigListRet = [];
            if (svnConfigList && svnConfigList.length > 0) {
                _.forEach(svnConfigList, (svnConfig) => {
                    svnConfig = Object.assign({}, JSON.parse(svnConfig.jsonStr));
                    delete svnConfig.jsonStr;
                    svnConfigListRet.push(svnConfig);
                });
            }
            this.svnProjectList = svnConfigListRet;
        },
    },
});
